package com.miyake.dsp.converter;

import com.miyake.dsp.ComplexArray;
import com.miyake.dsp.Processor;

public class NormalizeImpl implements Processor {

	private ComplexArray source;
	private double max;
	private double min;

	@Override
	public void setSource(ComplexArray source) {
		this.source = source;
	}

	@Override
	public ComplexArray getSource() {
		return this.source;
	}

	@Override
	public ComplexArray getProcessed() {
		getMinMax(source.getRealPart());
		double[] ret = new double[source.getRealPart().length];
		for (int i = 0; i < source.getRealPart().length; i++) {
			ret[i] = (source.getRealPart()[i] - min) / (max-min);
		}
//		getMinMax(ret);
		return new ComplexArray(ret, null);
	}

	private void getMinMax(double[] d) {
		max = d[0];
		min = d[0];
		for (int i = 0; i < d.length; i++) {
			if (max < d[i]) {
				max = d[i];
			}
			if (min > d[i]) {
				min = d[i];
			}
		}
	}

	@Override
	public Object getSetting() {
		return new NormalizeSetting();
	}
}
